package com.mc.moring.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseActivity;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.base.SaveZiLiPageTool;
import com.mc.moring.bean.LastLoanDetailBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.bean.getWebLinkConfigBean;
import com.mc.moring.information.modle.KycModle;
import com.mc.moring.login.LoginActivity;
import com.mc.moring.login.VerifyActivity;
import com.mc.moring.main.modle.LoanModle;
import com.mc.moring.util.AppManager;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.SPUtils;
import com.mc.moring.util.SharePreUtil;
import com.mc.moring.util.T;
import com.mc.moring.util.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class SettingActivity extends BaseActivity {
    private RelativeLayout layout_cam, layout_des, layout_cso, layout_cer;
    private ImageButton abBackIBtn;
    private TextView abTitleTV, btn_cer;
    public KycModle kycModle = null;

    private String mobile = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logE("setting page");
        setContentView(R.layout.activity_my_setting);
        getWebLinkConfigBean bean = GsonUtils.INSTANCE.getGson().fromJson(SharePreUtil.getStringData("_getWebLinkConfig"), getWebLinkConfigBean.class);
        abBackIBtn = findViewById(R.id.abBackIBtn);

        abTitleTV = findViewById(R.id.abTitleTV);
        abTitleTV.setText("Configuración");
        abBackIBtn.setOnClickListener(v -> finish());
        layout_cam = findViewById(R.id.layout_cam);
        layout_des = findViewById(R.id.layout_des);
        layout_cso = findViewById(R.id.layout_cso);
        layout_cer = findViewById(R.id.layout_cer);
        layout_cam.setOnClickListener(v -> {
            resetPwd();
        });
        kycModle = KycModle.Companion.getInstant();

        layout_des.setOnClickListener(v -> {
            PointMgr.INSTANCE.sendEvent("cl_person_infoofi_open");
            Log.e("PointMgr.INSTANCE.sendEvent","cl_person_infoofi_open");
            Intent intent = new Intent();
            intent.setClass(this, WebViewActivity.class);
            intent.putExtra("url", bean.getData().getDeactivateUrl());
            startActivity(intent);
        });
        layout_cer.setOnClickListener(v -> {


            PointMgr.INSTANCE.sendEvent("cl_person_infoof-15_open");
            Log.e("PointMgr.INSTANCE.sendEvent","cl_person_infoof-15_open");
            SharePreUtil.saveStringData("user_info", "cl_person_infoof-15_open");
            new SaveZiLiPageTool(this).puttKeyValue("user_info", "");
            SPUtils.INSTANCE.setPhone(null);
            ZiLiPageUserDataBean.setInstance(null);
            Thread thread = new Mythread();
            thread.start();
        });
        layout_cso.setOnClickListener(v -> {
            ToastUtils.showShort(SettingActivity.this, "three");
        });
    }

    public class Mythread extends Thread{
        @Override
        public void run(){
            try {
                sleep(1000);
                AppManager.getAppManager().finishActivity(MainActivity.class);
                finish();
                Intent intent = new Intent(SettingActivity.this, MainActivity.class);//这里修改退出的页面
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public Activity getActivity() {
        return null;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    private void resetPwd() {
        PointMgr.INSTANCE.sendEvent("cl_person_infoof-14_open");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_person_infoof-14_open");
        Map<String, String> params = new HashMap<String, String>();
        params.put("custId", ZiLiPageUserDataBean.getInstance().getCustomerId());
        LogUtils.logE("--- getZiLiPageUserFullInfo custId:"+ZiLiPageUserDataBean.getInstance().getCustomerId());
        showLoading();
        kycModle.getZiLiPageUserFullInfo(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if (kycModle.isSuccess(jsonObject)) {
                        JSONObject dataJSONObject = jsonObject.optJSONObject("data");
                        if (dataJSONObject != null) {
                            mobile = dataJSONObject.optString("mobile");
                            LogUtils.logE("--- getZiLiPageUserFullInfo mobile:"+mobile);
                            if (mobile != null) {
                                Intent intent = new Intent(SettingActivity.this, VerifyActivity.class);
                                intent.putExtra("account", mobile);
                                intent.putExtra("type", "1");
                                intent.putExtra("scenes","resetPassword");
                                finish();
                                startActivity(intent);
                            }
                        }
                    } else {
                        T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                hideLoading();
            }
        });
    }



    @Override
    protected void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<String, String>();
        showLoading();
        LoanModle.Companion.getInstant().loangetLastUnpaidOffLoan(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                LogUtils.logE("request LastUnpaidOffLoan success data>" + data);
                try {
                    LastLoanDetailBean bean = GsonUtils.INSTANCE.getGson().fromJson(data, LastLoanDetailBean.class);
                    if (bean != null) {
                        if (bean.getCode().equals("3101")) {
                            SharePreUtil.saveStringData("user_info", "");
                            new SaveZiLiPageTool(SettingActivity.this).puttKeyValue("user_info", "");
                            ZiLiPageUserDataBean.setInstance(null);
                            AppManager.getAppManager().finishActivity(MainActivity.class);
                            finish();
                            startActivity(LoginActivity.class);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                LogUtils.logE("e>" + e);
                hideLoading();
            }
        });
    }

    @Override
    public void onStop(){
        super.onStop();
        hideLoading();
    }
}
